const app = getApp()
Page({
  data:{
    orderInfo:null,
    returnReason: ['请选择'],
    returnIndex:0,
    tempFilePaths:'',
    isSeva:false,
    uploadImg:false
  },
  onLoad:function(e){
    var that = this
    wx.request({
      url: app.urlData.getURL+'/index.php/mobile/Ordertry/order_detail',
      method: 'POST',
      data: {
        id: e.id,
      },
      header: { 'Content-Type': 'application/json' },
      success: (res) => {
        console.log(res.data);
        that.setData({
          orderInfo: res.data
        })
      }
    })
    wx.request({
      url: app.urlData.getURL+'/index.php/mobile/Ordertry/refund_type',
      method: 'POST',
      header: { 'Content-Type': 'application/json' },
      success: (res) => {
        console.log(res.data);
        that.setData({
          returnReason: that.data.returnReason.concat(res.data)
        })
      }
    })
  },
  stateChange:function(e){
    this.setData({
      goodIndex: e.detail.value
    })
  },
  rutuChange:function(e){
    this.setData({
      returnIndex: e.detail.value
    })
  },
  textareChange:function(e){
    this.setData({
      returnIndex: e.detail.value
    })
  },
  chooseimage: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#333333",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })

  },
  chooseWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      count:1,
      sizeType: ['compressed'],
      sourceType: [type],
      success: function (res) {
        console.log('选取图片',res.tempFilePaths[0])
        var pach = res.tempFilePaths[0]
        wx.uploadFile({
          url: app.urlData.getURL +'/index.php/mobile/Ordertry/refund_img',
          filePath: pach,
          name: 'refund_img',
          header: {"Content-Type": "multipart/form-data",'accept': 'application/json'},
          formData:{
            'refund_img': pach
          },
          success: function (res) {
            console.log('上传结果',res) 
            that.setData({
              tempFilePaths: res.data
            })
          }
        })
      }
    })
  },
  userSave:function(e){
    var that = this
    var tab = e.detail.value
    if (tab.refund_type == 0){
      wx.showToast({
        title: '请选择原因',
        icon: 'clear',
        duration: 1000
      })
      return;
    }
    if (tab.refund_desc == '') {
      wx.showToast({
        title: '请描述具体原因',
        icon: 'clear',
        duration: 1000
      })
      return;
    }
    console.log(e)
    if(that.data.isSeva == false){
      that.setData({
        isSeva:true
      })
      wx.request({
        url: app.urlData.getURL +'/index.php/mobile/Ordertry/application_refund_order',
        method: 'POST',
        header: { 'Content-Type': 'application/json' },
        data: {
          'id': that.data.orderInfo.id,
          'user_id': wx.getStorageSync('user_id'),
          'refund_imgs': that.data.uploadImg == true ? 'http://h-cdn.zealcdn.cn/' + that.data.tempFilePaths : '',
          'refund_type': tab.refund_type,
          "refund_desc": tab.refund_desc,
        },
        success: (res) => {
          console.log(res.data);
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 1000
          })
        }
      })
      setTimeout(function(){
        that.setData({
          isSeva: false
        })
      },200)
    }
  }
})